/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.model.DiagramInfo;
import edu.pku.sei.modeler.jet.util.ModelerConstants;

/**
 * @author zl
 *
 */
public class GenActions extends AbstractGenerator {
	private IFolder folder;
	private List<DiagramInfo>dInfoList;
	
	public void setDiagInfos(List<DiagramInfo> dInfoList) {
		this.dInfoList = dInfoList;
	}
	/* (non-Javadoc)
	 * @see edu.pku.sei.modeler.jet.codegen.AbstractGenerator#generateCode(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void generateCode(IProgressMonitor monitor)
			throws InterruptedException {
		assert proj != null;
		assert dInfoList != null;
		String codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.actionBase;
		
		folder = proj.getFolder("src/"+codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create action folder failed");
		}
		genActionProvider(monitor);
	}

	private translated.ActionProviderGen genActionProvider = translated.ActionProviderGen.create("\n");
	public void genActionProvider(IProgressMonitor monitor)
			throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/actions/ActionProvider.javajet";

			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri,
					Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/
//			String result = emitter.generate(monitor,
//					new Object[] { dInfoList });
			String result = genActionProvider.generate(dInfoList);
			IFile javaFile = folder.getFile(ModelerConstants.actionPrivider
					+ ".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result
					.getBytes());
			javaFile.create(is, true, monitor);
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate action provider failed");
		}
	}
}
